package com.athuiju.fenxiaoback.controller;


import com.athuiju.fenxiaoback.entity.TbSku;
import com.athuiju.fenxiaoback.entity.TbSpu;
import com.athuiju.fenxiaoback.entity.UmsMemberCollectSpu;
import com.athuiju.fenxiaoback.entity.UmsMemberReceiveAddress;
import com.athuiju.fenxiaoback.service.TbSkuService;
import com.athuiju.fenxiaoback.service.TbSpuService;
import com.athuiju.fenxiaoback.service.UmsMemberCollectSpuService;
import com.athuiju.fenxiaoback.utils.JwtUtils;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员收藏的商品 前端控制器
 * </p>
 *
 * @author athuiju
 * @since 2021-07-16
 */
@Api(description="收藏",tags = "收藏")
@RestController
@RequestMapping("/fenxiaoback/ums-member-collect-spu")
public class UmsMemberCollectSpuController {


    @Autowired
    UmsMemberCollectSpuService collectSpuService;

    @Autowired
    TbSpuService spuService;

    @Autowired
    TbSkuService skuService;

    @ApiOperation(value = "商品名查询收藏列表里的商品")
    @GetMapping("selectBySpuName/{spuName}")
    public R selectBySpuName(@PathVariable String spuName,HttpServletRequest request){
        String memberid = JwtUtils.getMemberIdByJwtToken(request);

        QueryWrapper<UmsMemberCollectSpu> wrapper=new QueryWrapper<>();

        wrapper.eq("member_id",memberid);
        wrapper.like("spu_name",spuName);
        List<UmsMemberCollectSpu> list = collectSpuService.list(wrapper);


        return R.ok().put("data", list);
    }




    @ApiOperation(value = "获取用户收藏")
    @GetMapping("getCollects")
    public R getCollects(HttpServletRequest request){
        String memberid = JwtUtils.getMemberIdByJwtToken(request);

        QueryWrapper<UmsMemberCollectSpu> wrapper=new QueryWrapper<>();

        wrapper.eq("member_id",memberid);
        wrapper.orderByAsc("create_time");
        List<UmsMemberCollectSpu> list = collectSpuService.list(wrapper);
        return R.ok().put("data", list);
    }



    @ApiOperation(value = "删除收藏")
    @DeleteMapping("delCollect")
    public R delCollect(@RequestBody List<Long> spuids, HttpServletRequest request) {
        String memberid = JwtUtils.getMemberIdByJwtToken(request);
        collectSpuService.delCollects(spuids,memberid);

        return R.ok().put("msg", "删除成功");
    }


    @ApiOperation(value = "添加收藏")
    @PostMapping("addCollect/{spuid}")
    public R addCollect(@PathVariable String spuid, HttpServletRequest request) {
        String memberid = JwtUtils.getMemberIdByJwtToken(request);

        UmsMemberCollectSpu collect = collectSpuService.getOne(new QueryWrapper<UmsMemberCollectSpu>().eq("spu_id", spuid).eq("member_id", memberid));
        if (collect!=null){
            return R.error().put("msg","请勿重复添加");
        }

        TbSpu spu = spuService.getById(spuid);


        UmsMemberCollectSpu collectSpu=new UmsMemberCollectSpu();
        collectSpu.setMemberId(Long.parseLong( memberid));
        collectSpu.setSpuId(Long.parseLong(spuid));
       if (StringUtils.isEmpty(spu.getSpuImage())){
           collectSpu.setSpuImg("http://huijujx.365hy.com/fenxiaoimages/goodimages/good@2x.png");
       }else{
           String[] split = spu.getSpuImage().split(",");
           collectSpu.setSpuImg(split[0]);
       }

        collectSpu.setSpuName(spu.getTitle());
        collectSpu.setCreateTime(new Date());

        List<TbSku> skus = skuService.list(new QueryWrapper<TbSku>().eq("spu_id", spu.getId()));
        if (skus!=null && skus.size()>0){
            //找到价格最低的sku 放入收藏
            Long minPrice=skus.get(0).getPrice();
            for (int i=0;i<skus.size();i++){
                Long price = skus.get(i).getPrice();
                if (price < minPrice){
                    minPrice=price;
                }
            }
            collectSpu.setPrice(minPrice);
        }else{
            return R.error().put("msg","这个spu没有一个sku,收藏失败");
        }

        collectSpuService.save(collectSpu);

        return  R.ok().put("msg", "收藏成功");
    }


}

